SmartClick Tutorial by Philip

1 - THE REASON FOR SMART CLICKS:

QuickJump: Back to Library - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - Bonus

In the early versions of AI Wars, all commands—math, scans, movements, etc.—took the same length of time. Each command took one turn or one "click." This discouraged cybugs from doing too much thinking and rewarded those bugs that were more action oriented. Even simple scans were compromised. Consider this sequence:
 

The condition would be met because the last scan had found an enemy. A missile would be launched, even though the enemy had moved.

Cybugs that engaged in a complicated series of scans and computations—in other words, the more intelligent cybugs—often found themselves several clicks behind their more impulsive opponents.

In order to encourage more sophisticated artificial intelligence, recent versions of AI Wars have employed a system of smart clicks. With smart clicks, different kinds of commands take different amounts of time, enabling a cybug to complete multiple scans and logic commands in the time it takes to move or use a weapon.
 
2 - HOW IT WORKS:

QuickJump: Back to Library - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - Bonus

With smart clicks turned on, each cybug's turn still lasts one click, but each click is divided into 10 subclicks. And with proper subclick management, you can lengthen a click to as many of 19 subclicks.
 

At the beginning of a cybug's turn, AI Wars assembles a list of commands that it will execute during that turn. It continues to add to the list of commands until the total number of subclicks for those commands equals or exceeds 10.

Examples:

1. The first command on the list is "launch missile," an action. The total number of subclicks for that one command is 10, and it will be the only command executed during that turn.

2. The first command on the list is "scan perimeter." Because a scan takes fewer than 10 subclicks (it takes 3 subclicks), AI Wars adds another command. The second command is "math v1 = #x_pos + 1" (1 subclick). Because the total number of subclicks is still less than 10, AI Wars adds a third command. The third command is "move forward." Now the total number of subclicks for all the commands is 14 (3 + 1 + 10). Because this number exceeds 10, these are all of the commands that will be executed during this turn.

3 - HOW TO OPTIMIZE THE EFFICIENCY OF A CYBUG WITH SMART CLICKS:

QuickJump: Back to Library - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - Bonus

Once you understand how smart clicks work, it isn't difficult to see that by counting subclicks and arranging commands properly, you can increase the number of commands your cybug will execute. If each click begins with 9 subclicks' worth of scans and logic commands followed by an action command, you can maximize the number of subclicks available to your cybug.

Here is an example of a routine that uses the maximum number of subclicks. It is a simple routine for moving a cybug forward while performing some basic checks. Later, we'll learn a few tricks to make this routine even more efficient.
 
subclick 2: move_routine: 
subclick 3-5: long range scan 
subclick 6: if scan found enemy then goto attack_routine 
subclick 7: if scan found flag then goto flag_routine 
subclick 8: if scan found mine then goto beware_routine 
subclick 9-18: move forward 
subclick 0: if bump barrier then goto barrier_routine 
subclick 1: goto move_routine
 
The subclick numbers on the left indicate which subclick(s) in the click each command will occupy.

First look at subclick 0 (near the bottom of the loop). Any command that follows an action command will always be subclick 0 (the first subclick in a click). The reason for this is that, if there is an action command in a click, it will always be the last command executed in that click. Once an action command (a 10 subclick command) has been executed, the total number of subclicks for that clicks must be at least 10, so the next command cannot be executed until the next click.

Now, follow the routine. Notice that it takes two subclicks (subclicks 1 and 2) to loop back up to the top of the routine—one for the goto and one for the label.

The scan then takes three subclicks (subclicks 3, 4, and 5), followed by three single-subclick commands (which occupy subclicks 6, 7, and 8).

This brings us to subclick 9. Whatever command your cybug executes at subclick 9 is going to be the last command executed during this click. It doesn't matter whether it's a small single-subclick logic command or a major 10-subclick action command. Any command—no matter what its length—is going to push you over the 10-subclick threshold, so you might as well make it a big one. Whenever possible put your action commands at subclick 9.

4 - SMART CLICK TRICKS AND TECHNIQUES:

QuickJump: Back to Library - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - Bonus

You probably won't use every one of these tricks and techniques in every routine, but they all have uses in certain situations. And if you use a few of them, your cybugs will soon become more efficient than you ever thought possible.
 
5 - CHECKING YOUR GOTOS:

QuickJump: Back to Library - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - Bonus

One of the simplest ways to "steal back a subclick" is to combine one of your if/then checks with the goto at the bottom of your loop. Here is how you would do that with our example from above.
 
 
subclick 1: move_routine: 
subclick 2-4: long range scan 
subclick 5: if scan found enemy then goto attack_routine
subclick 6: if scan found flag then goto flag_routine 
subclick 7: if scan found mine then goto beware_routine 
subclick 8: [extra subclick] 
subclick 9-18: move forward 
subclick 0: if not bump barrier then goto move_routine 
goto barrier_routine
 
In the original version of this routine, we included a condition (if bump barrier . . .) that if met would take us out of the routine (. . . then goto barrier_routine). In this version we applied the negative (if not bump barrier . . .) as a condition for remaining in the routine (. . . then goto move_routine). This accomplishes the same thing and saves one subclick.

If the cybug does not bump into a barrier, it loops back up to the top of the routine. If the cybug does bump into a barrier, it will not goto the top of the routine and will instead goto the barrier_routine. (An even more efficient way to do this would be to position the barrier_routine immediately after the move_routine so the cybug would move directly into it without needing to execute the goto command.)

Everything else in the routine is the same, and there is now an extra subclick (subclick 8) to do with as you please!
 
6 - VARIABLES IN YOUR GOTOS AND LABELS:

QuickJump: Back to Library - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - Bonus

Here's a way to consolidate a lot of if/then . . . goto . . . statements by taking advantage of system variables—in this case #scan.
 
subclick 1: move_routine: 
subclick 2-4: long range scan 
subclick 5: goto routine #scan 
subclick 6: [extra subclick] 
subclick 7: [extra subclick] 
subclick 8: [extra subclick] 
subclick 9-18: move forward 
subclick 0: if not bump barrier then goto move_routine 
[Barrier Navigation Routine]
 
In our earlier versions, we used three separate if/then statement to call subroutines for attacking, getting a flag, and dealing with a mine. Here we have accomplished the same thing in one line.

If our scan found an enemy, the #scan variable will equal 2. So instead of using "if scan found enemy then goto attack_routine," we'll change the label from attack_routine to routine 2. This "goto routine #scan" will send us there when we scan an enemy. And the same command will send us to routine 3 (for mines) and routine 5 (for flags) when appropriate.

But what will happen if we scan a barrier or a friend (in which case #scan would equal 1 or 4) and we don't have a routine for those? If you attempt to goto a routine that does not exist in your code, AI Wars will simply ignore the command (although it will still take one subclick).

Our routine still does everything the original did, but we now have three extra subclicks to play with

7 - SAVING SUBCLICKS BY REPLACING SCANS WITH SYSTEM VARIABLE CHECKS:

QuickJump: Back to Library - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - Bonus

Some information is available both from scans and from system variables (primarily #x_pos, #y_pos, #enemy_x, and #enemy_y). Because the logic statements used to evaluate variables take only one subclick, and scans take three subclicks, system variables are often the preferable source.

One of the most common uses of system variables as a substitute (or, in many cases, a pre-check) for scans is to check whether the enemy is on the same axis as your cybug.

If your cybug were using long range scans to check to see if an enemy was behind it or to the side of it, your cybug would need to turn and scan three times and then turn once more in order to face the original direction. This would take 4 full clicks!

But by comparing system variables, your cybug could tell in two subclicks whether there was any cause for concern.
 

If #enemy_x does not equal #x_pos, then you know that the enemy is not directly north or south of your cybug. Likewise, if #enemy_y does not equal #y_pos then the enemy is not directly east or west of your cybug.

There are limitations to using system variables to replace scans:
 

A second scan that can be replaced with a logic command involving system variables is the perimeter scan. If you were going to scan to check for an enemy on your perimeter, you would use four subclicks—three for the scan, and two to check the results of the scan. Like this: But you could also use a cmath formula to detect an enemy on your perimeter and save two subclicks. Here is one way to do so: Precisely how this works is the subject of another discussion, but it's based on the Pythagorean Theorem (a² + b² = c²).
 
8 - CONCLUSION:

QuickJump: Back to Library - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - Bonus

These are some of the basic techniques for maximizing the efficiency of your Cybug by managing subclicks. Learn to use them, but more importantly, develop an overall understanding of the Smartclick system. As you develop this understanding, you'll find unique ways to tweak your code.